<!-- 预警 -->
<template>
	<el-drawer v-model="isShowDrawer" direction="rtl" :before-close="handleClose" :size="curDrawerSize" :close-on-click-modal="isReadOnly">
		<template #header="{ titleId, titleClass }">
			<div class="zs-drawer-header" :id="titleId" :class="titleClass">
				<i class="icon-b-item zs-icon zs-text-blue zs-space-right-sm"></i>
				<div class="zs-title zs-text-black">预警设置</div>
			</div>
		</template>

		<template #default>
			<el-container class="zs-drawer-container">
				<el-scrollbar class="zs-drawer-content">
					<!-- card1 -->
					<div class="zs-align-center zs-space-bottom" id="sm-drawer-demo-card-01">
						<i class="icon-s-item zs-icon zs-text-blue zs-space-right-sm"></i>
						<div class="zs-title">预警设置</div>
					</div>
					<div class="zs-card zs-space-bottom">
						<HsForm :filed-list="warningFiledList" ref="infoForm" @selectChange="selectChange" :isDisabled="isReadOnly"></HsForm>
					</div>
					<!-- card2 -->
					<CreateUserInfo v-if="handleType == 'info'" ref="createDataRef" title="创建信息" id="create-user-info" />
				</el-scrollbar>
				<hsDocWrapper :nodeList="curNodeList" />
			</el-container>
		</template>

		<template v-if="handleType != 'info'" #footer>
			<div>
				<el-button class="zs-btn-md zs-btn-primary" @click="doConfirm">提交</el-button>
			</div>
		</template>
	</el-drawer>
</template>

<script setup>
	import HsForm from '@/components/hsForm.vue';
	import CreateUserInfo from '@/components/createUserInfo.vue';
	import { warningFiledList } from '@/config/form/supplies.js';
	import { warningApi, warningInfoApi } from '@/api/supplies.js';
	import { processListApi } from '@/api/common.js';
	import { ElMessage } from 'element-plus';
	import { ref, reactive, watch, nextTick } from 'vue';
	import { useStore } from 'vuex';
	import { useRoute } from 'vue-router';

	const route = useRoute();

	const props = defineProps({
		isShow: { type: Boolean, default: false },
		isReadOnly: { type: Boolean, default: true },
		handleType: { type: String, default: '' },
		currentRow: { type: Object },
	});

	let store = useStore();
	const curDrawerSize = ref(store.state.uiDrawerSize);
	const curTextareaLength = ref(store.state.uiTextareaLength);

	const options = [{ dictLabel: 1, dictValue: 1 }];

	const emit = defineEmits(['close']);

	let isShowDrawer = ref(props.isShow);

	const curNodeList = ref([{ name: '预警设置', id: 'sm-drawer-demo-card-01' }]);

	const infoForm = ref();
	let createDataRef = ref();

	watch(
		() => props.isShow,
		(newVal, oldVal) => {
			isShowDrawer.value = newVal;
			nextTick(() => {
				newVal && props.handleType == 'add' && initForm();
				newVal && ['info', 'edit'].includes(props.handleType) && getInfo(props.currentRow?.id);
			});
		},
	);

	//详情
	const getInfo = async id => {
		let res = await warningInfoApi({id});
		if (res.data.code !== 200) return;
		let existCreate = curNodeList.value.find(item => item.id === 'create-user-info');
		!existCreate && curNodeList.value.push({ name: '创建信息', id: 'create-user-info' });
		infoForm.value.setFiledVal(res.data.data)
		nextTick(() => {
			props.handleType == "info" && createDataRef.value.setData(res.data.data);
		});
	};

	//提交
	const doConfirm = async () => {
		let model = await infoForm.value.validateForm();
		if (!model) return;
		model.id = props.currentRow.id;
		let res = await warningApi(model);
		if (res.data.code == 200) {
			ElMessage({
				message: '提交成功',
				type: 'success',
			});
			isShowDrawer.value = false;
			emit('close', { isShow: false, isRefresh: true });
		}
	};

	//关闭抽屉
	const handleClose = done => {
		emit('close', { isShow: false });
		done();
	};

	//表单初始化
	const initForm = () => {
		infoForm.value.resetFields();
		let existCreate = curNodeList.value.find(item => item.id === 'create-user-info');
		existCreate && curNodeList.value.pop();
	};

	//表单关联
	const selectChange = data => {
		if (data.filedName == "warnStatus") {
            data.filedVal == 0 && infoForm.value.setFiledSource("warnRule",0)
            data.filedVal == 0 && infoForm.value.setFiledSource("warnNum",0)
        }
		if (data.filedName == "warnRule") {
            data.filedVal == 0 && infoForm.value.setFiledSource("warnNum",0)
        }
    };
</script>
